<%@ page import="java.util.Objects" %>
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>结果显示</title>
</head>
<body>
<h2>数据查询结果</h2>
<%
    request.setCharacterEncoding("utf-8");
    String username="root";
    String pwd="pengfei";
    String driverName="com.mysql.jdbc.Driver";
    String dbName="student";
    String url="jdbc:mysql://127.0.0.1:3306/"+dbName+"?user="+username+"&password="+pwd+"&useUnicode=true&characterEncoding=UTF-8";
    String sql="";
    Connection connection = null;
    ResultSet rs = null;
    PreparedStatement st = null;
    try {
        Class.forName(driverName);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    try {
        connection= DriverManager.getConnection(url);

        if(!request.getParameter("id").equals("")) {
            int id = Integer.parseInt(request.getParameter("id"));
            sql = sql + " and id=" + id;
        }
        if(!request.getParameter("name").equals("")) {
            String name=request.getParameter("name");
            sql = sql + " and name='" + name + "'";
        }
        if((!Objects.equals(request.getParameter("weight1"), "")) && (!Objects.equals(request.getParameter("weight2"), ""))) {
            float weight1= Float.parseFloat(request.getParameter("weight1"));
            float weight2= Float.parseFloat(request.getParameter("weight2"));
            sql = sql + " and weight between " + weight1 + " and " + weight2;
        }
        sql="select * from stu_info where 1=1"+sql;

        st=connection.prepareStatement(sql);
        rs=st.executeQuery(sql);

    } catch (SQLException e) {
        e.printStackTrace();
    }

%>
<table border="1" style="text-align: center">
<tr>
    <td>记录条数</td><td>学号</td><td>姓名</td><td>性别</td><td>年龄</td><td>体重</td><td>身高</td>
</tr>
<%  while(rs.next()) {%>
<tr>
    <td><%=rs.getRow() %></td><td><%=rs.getString("id") %></td>
    <td><%=rs.getString("name") %></td><td><%=rs.getString("sex") %></td>
    <td><%=rs.getString("age") %></td><td><%=rs.getString("weight") %></td>
    <td><%=rs.getString("height") %></td>
</tr>
<% }
    if(rs!=null) rs.close();
    if(st!=null) st.close();
    if(connection!=null) connection.close();
%>
</table>

</body>
</html>
